package com.ari.actions;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.devicemantra.coordinate.dao.Settings;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class EPPDViewServers {

	static final Logger logger = Logger.getLogger(EPPDViewServers.class);
	@Expose
	@SerializedName("eppd_servers")
	private List<Settings> eppdServerList = null;

	private SessionFactory sessionFactory;

	public List<Settings> getEppdServerList() {
		return eppdServerList;
	}

	@Transactional
	public String execute() {
		logger.info("*********************" + this);

		try {

			logger.info("Sleeping for 4 seconds");
			Thread.sleep(1000);
		} catch (final InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		/* Query the db now */
		try {
			final Session session = sessionFactory.getCurrentSession();
			final Query query = session.createQuery("from Settings");
			eppdServerList = query.list();
			final Iterator<Settings> iterator = eppdServerList.iterator();
			for (final Settings item : eppdServerList) {
				System.out.println("item " + item.getEppdIp());
				System.out.println("item " + item.getJksFileBytes().length);
			}

		} catch (final HibernateException e) {
			System.out.println("Rolling back");
			e.printStackTrace();
		}
		return "view";
	}

	public void setSessionFactory(final SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

}
